from PyQt5.QtWidgets import *
from PyQt5 import QtGui
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import pymysql
import base64
import io
import PIL.Image
import interface.admin.stuadd as stuadd
import database.admin.setting as setting
class stuadd_window(QMainWindow,stuadd.Ui_Dialog):
    def __init__(self):
        QMainWindow.__init__(self)
        stuadd.Ui_Dialog.__init__(self)
        self.setupUi(self)
        self.setWindowTitle("添加学生信息")
        self.quit.clicked.connect(self.close)
        self.commit.clicked.connect(self.commit_add)
        self.grade_box.addItem('ALL')
        self.grade_box.addItem('大一')
        self.grade_box.addItem('大二')
        self.grade_box.addItem('大三')
        self.grade_box.addItem('大四')
        self.school_box.addItem('ALL')
        self.school_box.addItem("航空学院")
        self.school_box.addItem("航天学院")
        self.school_box.addItem("航海学院")
        self.school_box.addItem("材料学院")
        self.school_box.addItem("机电学院")
        self.school_box.addItem("动力与能源学院")
        self.school_box.addItem("电信学院")
        self.school_box.addItem("计算机学院")
        self.school_box.addItem("外国语学院")
        self.school_box.addItem("软件学院")
        self.school_box.addItem("管理学院")
        self.insert_img.clicked.connect(self.add_img)
    def add_img(self):
        img_path, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "All Files(*);;*.jpg;;*.png;;*.jpeg")
        print(img_path)
        if img_path!='':
            with open(img_path, 'rb') as f:
                img = f.read()
            img = base64.b64encode(img)
            map_= QtGui.QPixmap(img_path)
            self.dis_img.setPixmap(map_)
            self.dis_img.setScaledContents(True)
            self.img=img
    def print_succ(self):
        pass
    def commit_add(self):
        id_= self.id.text()
        name_ = self.name.text()
        if(self.male.isChecked()):
            sex_='男'
        else:
            sex_='女'
        grade_=self.grade_box.currentText()
        school_=self.school_box.currentText()
        major_=self.ma.text()
        info=(id_,name_,sex_,grade_,school_)
        print(info)
        self.connect = pymysql.connect(**setting.config,charset='utf8')
        self.cursor = self.connect.cursor()
        info = (id_, name_, sex_, grade_, school_,major_)
        print(info)
        img=self.img
        img=pymysql.Binary(img)
        print(img)
        print(type(img))
        sql_="Insert INTO students (id,name,sex,grade,school,major)VALUES("+"'%s',"%id_+"'%s'," %name_+"'%s',"%sex_+"'%s',"%grade_+"'%s',"%school_+"'%s');"%major_
        #print(sql_)
        #sql_ = "Insert into students (id,name,sex,grade,school)values (%s,%s,%s,%s,%s)" % ('1', '33','女', 's', 'sa')
        self.cursor.execute(sql_)
        #self.cursor.execute("INSERT INTO students SET img='%s' where id=%s" %((img), id_))
        #re=self.cursor.fetchall()
        #print(re)
        self.connect.commit()
        sql1 = "update students set picture=(%s) where id=(%s)"
        #print(sql1)
        arg=(img,id_)
        self.cursor.execute(sql1,arg)
        self.connect.commit()
        sql_2="select picture from students where id=%s"%id_
        self.cursor.execute(sql_2)
        data = self.cursor.fetchall()
        data1 = base64.b64decode(data[0][0])
        file_like = io.BytesIO(data1)
        img = PIL.Image.open(file_like)
        #img.show()
        #####设置触发器后可以删除
        #sql_="Insert into id_passwd values(%s,'123456','学生') "
        #self.cursor.execute(sql_,id_)
        #self.connect.commit()
        #####
        self.connect.close()
        self.cursor.close()
        QMessageBox.information(self, "提示", "录入学生信息成功！")
        self.close()